"""
初始账户（username:root，passwd:123） （username:admin，passwd:admin）
"""
user = {"root": {"passwd": "123", "money": 1000},"admin": {"passwd": "admin", "money": 1000}}
print("-" * 50)
print('#' + "{0:^42}".format("欢迎登录三乐购物系统") + '#')
print("-" * 50)
print("温馨提示，输入quit退出系统")

while (1):
    username = input("请输入用户名：")
    if username == 'quit':
        break
    pwd = input("请输入密码：")
    if username not in user.keys():
        print("用户名不存在")
        continue

    elif user[username]['passwd'] == pwd:
        print("登录成功!")
        have_money = user[username]['money']
        print("-" * 50)
        product = {'f001': {"name": "apple", "price": 2.5},
                   'f002': {"name": "banana", "price": 3.5},
                   'f003': {"name": "grape", "price": 4.5},
                   'f004':{"name":"fork","price":18},
                   'f005':{"name":"sheep","price":25}}

        print("商品列表")
        for i, j in product.items():
            print(f"编号：{i}，名称：{j['name']},单价：{j['price']}")

        buy_pro = []
        sum_price = 0.0
        print("输入quit退出购买")
        while (1):
            buy = input("请输入要购买的物品编号:")
            if buy == 'quit':
                break
            if buy not in product.keys():
                print("输入有误，请重新输入")
                continue

            Input=input("请输入要购买的数量:")
            if Input.isdigit():
               count = float(Input)
            else:
                print("输入有误，请重新输入")
                continue

            have_money = have_money - product[buy]['price'] * count
            if  have_money < 0:
                print("余额不足不可以购买")
                have_money = have_money + product[buy]['price'] * count
                continue
            else:
                print(f"你的余额为{have_money}")
                sum_price = product[buy]["price"] * count + sum_price
                buy_pro.append([product[buy]['name'], count])

        print("-" * 50)

        print("已购买的商品：")
        for i in buy_pro:
            print(f"名称：{i[0]} 数量：{i[1]}")

        print(f"总购买金额为:{sum_price}")

        button=input("购买结束，是否选择支付，输入yes付款，no退出系统:")
        if button == "no":
            break
        else:
            if user[username]["money"] >= sum_price:
                user[username]["money"] -= sum_price
                print("支付成功！")
                print(f"余额：{user[username]['money']}")
            else:
                print("您的余额不足，无法支付")
            break
    else:
        print("密码错误")
        continue




